package com.sdry.job;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.sdry.entity.PartComponent;
import com.sdry.entity.PartComponentExample;
import com.sdry.entity.PartInfo;
import com.sdry.entity.PartInfoExample;
import com.sdry.entity.PartInfoExample.Criteria;
import com.sdry.entity.Stagnant;
import com.sdry.entity.StagnantExample;
import com.sdry.service.PartComponentService;
import com.sdry.service.PartInfoService;
import com.sdry.service.StagnantServise;

/**
 * @AUTHOR HACKER
 */
public class PartJob {
	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory.getLogger(PartJob.class);
	
	@Autowired
	private PartComponentService partComponentService;
	@Autowired
	private StagnantServise stagnantServise;
	@Autowired
	private PartInfoService partInfoService;
	
	public void excute() throws ParseException {
		
		PartComponentExample partInfoExample = new PartComponentExample();
		//查询库存信息
		List<PartComponent> list = partComponentService.selectByExample(partInfoExample);
		
		//遍历库存
		for (PartComponent partComponent : list) {
			//根据备件id查询备件最大呆滞时间
			PartInfoExample example = new PartInfoExample();
			Criteria createCriteria = example.createCriteria();
			if(partComponent.getInfoId() != null){
				createCriteria.andInfoIdEqualTo(partComponent.getInfoId());
			}
			List<PartInfo> selectByExample = partInfoService.selectByExample(example );
			//最大呆滞时间
			if(selectByExample != null) {
				if(selectByExample.get(0).getMaxStoreTime() != null && selectByExample.get(0).getMaxStoreTime() != "") {
					String maxStoreTime = selectByExample.get(0).getMaxStoreTime();
					//入库时间:2018-08-01
					String cremark2 = partComponent.getCremark2();
					String substring = cremark2.substring(3,4);
					
					Integer valueOf = Integer.valueOf(substring);
					Integer valueOf2 = Integer.valueOf(maxStoreTime);
					//新时间 = 入库时间 + 最大呆滞时间
					Integer integer = valueOf + valueOf2;
					char ch = (char)(integer + '0');
					char charAt = cremark2.charAt(3);
					//新时间
					String replace = cremark2.replace(charAt, ch);
					SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
					Date parse = dateFormat.parse(replace);
					
					//获取当前时间
					Date date = new Date();
					String format = dateFormat.format(date);
					Date parse2 = dateFormat.parse(format);
					
					//新时间
					long time = parse.getTime();
					//当前时间
					long time2 = parse2.getTime();
					
					//用当前时间 - 新时间  
					if(time2 - time >= 0) {
						//如果>= 0，说明已经超过呆滞最大时间，说明是呆滞物 ，查询呆滞物表中存不存在此库存id，不存在则添加
						//库存id
						if(partComponent.getComponentId() != null) {
							Long componentId = partComponent.getComponentId();
							//根据库存id查询呆滞物表中是否存在此库存id
							StagnantExample example2 = new StagnantExample();
							com.sdry.entity.StagnantExample.Criteria createCriteria2 = example2.createCriteria();
							createCriteria2.andComponentIdEqualTo(componentId);
							List<Stagnant> stagnants = stagnantServise.selectByExample(example2  );
							
							if(stagnants.size() == 0) {
								Stagnant stagnant = new Stagnant();
								stagnant.setComponentId(componentId);
								stagnantServise.insert(stagnant );
							}
						}
					}
				}
			}
		}
		
		//LOGGER.info("quarts excute..." + list.size());
	}
}
